<template>
  <view>
    <view class="goods-list">
        <view v-for="(goods, i) in goodsList" :key="i" @click="gotoDetail(goods)">
        <my-goods :goods="goods"></my-goods>
        </view>
      </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj:{
          query:'',
          cid:'',
          pagenum:1,
          pagesize:10
        },
        goodsList:[],
        total:0,
       
         //设置节流
         isloading:false

      };
    },
    onLoad(options){
      console.log(options);
      this.queryObj.query=options.query||''
      this.queryObj.cid=options.cid||''
      this.getGoodsList()
    },
    methods:{
      async getGoodsList(){
        this.isloading=true
           const { data: res } = await uni.$http.get('goods/search', this.queryObj)
           this.isloading=false
           if(res.meta.status!==200) return uni.$showMsg('数据请求失败')
           this.goodsList=[...this.goodsList,...res.message.goods]
           this.total=res.message.total
      },
      gotoDetail(item){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      }
    },
    onReachBottom() {
      if(this.goodsList.length>=this.total) return uni.$showMsg('数据加载完毕')
      if(this.isloading===true) return 
      this.queryObj.pagenum++
      this.getGoodsList()
    },
    async onPullDownRefresh() {
      //下拉刷新先把一些参数重新还原初始化数据
      this.queryObj.pagenum=1
      this.total=0
      this.goodsList=[]
      this.isloading=false
      await this.getGoodsList()
      //等待数据请求完成关闭下拉刷新
      uni.stopPullDownRefresh()
    },
  
  }
</script>

<style lang="scss">
 

</style>
